package com.sm.ticket.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sm.ticket.feigns.UserFeignClient;
import com.sm.ticket.mapper.TicketDao;
import com.sm.tool.entity.po.Tickets_;
import com.sm.tool.entity.po.User_;
import com.sm.tool.entity.request.AddTicketsRequest;
import com.sm.tool.entity.request.PageInfo;
import com.sm.tool.entity.response.ReturnData;
import com.sm.tool.entity.vo.TicketsVO;
import com.sm.tool.enums.NumberEnums;
import com.sm.tool.enums.ResultEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TicketService {

    @Value("${ticket.type_prefix.discount}")
    private String discount;

    @Value("${ticket.type_prefix.vertical_reduction}")
    private String vertical_reduction;

    @Value("${ticket.type_prefix.full_red}")
    private String full_red;

    @Autowired
    private JedisCluster jedisCluster;

    @Autowired
    private TicketDao ticketDao;

    @Autowired
    private UserFeignClient userFeignClient;

    public ReturnData addTickets(String token, AddTicketsRequest addTicketsRequest) {
        /**
         * 0:满减券,1:折扣券,2:立减券
         */

        switch (addTicketsRequest.getType()) {
            case 0:
                addTicketsRequest.setTicketCode(full_red + System.currentTimeMillis());
                break;
            case 1:
                addTicketsRequest.setTicketCode(discount + System.currentTimeMillis());
                break;
            case 2:
                addTicketsRequest.setTicketCode(vertical_reduction + System.currentTimeMillis());
                break;
            default:
                return ReturnData.builder().build();
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ticket_code", addTicketsRequest.getTicketCode());
        if (ticketDao.selectCount(queryWrapper) != NumberEnums.ZERO.getNumber()) {
            return ReturnData.builder().code(ResultEnums.TICKET_CODE_REPEAT.getCode()).msg(ResultEnums.TICKET_CODE_REPEAT.getMessage()).build();
        }

        String adminUserId = jedisCluster.get(token);
        Tickets_ ticket_ = Tickets_.builder().build();
        BeanUtils.copyProperties(addTicketsRequest, ticket_);

        ticket_.setCreateId(Long.parseLong(adminUserId));
        ticketDao.insert(ticket_);
        return ReturnData.builder().build();
    }

    @PostConstruct
    void init(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ticket_code", full_red+"25yu1ss6xx6666t");
        if (ticketDao.selectCount(queryWrapper) == NumberEnums.ZERO.getNumber()) {

            ticketDao.insert(Tickets_.builder().fullRed("50,20")
                    .explains("注册有礼券")
                    .maxReceive(1)
                    .createId(new Long(0))
                    .number(100000)
                    .termOfValidity(30)
                    .ticketCode(full_red+"25yu1ss6xx6666t")
                    .ticketName("注册有礼满减券")
                    .type(0)
                    .useUserType(1)
                    .build());
        }

    }
    public ReturnData queryTickets(PageInfo pageInfo) {


        IPage<Tickets_> page = new Page(pageInfo.getCurrent(), pageInfo.getSize());

        QueryWrapper<Tickets_> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        IPage<Tickets_> ticketsIPage = ticketDao.selectPage(page, queryWrapper);

        List<Tickets_> ticketsList = ticketsIPage.getRecords();
        List<TicketsVO> ticketsVOList = new ArrayList<>();

        for (Tickets_ tickets_ : ticketsList) {
            TicketsVO ticketsVO = new TicketsVO();
            BeanUtils.copyProperties(tickets_, ticketsVO);

            User_ createUser = userFeignClient.getUserInfo(tickets_.getCreateId());
            ticketsVO.setCreateUserNickName(createUser.getNickName());
            ticketsVOList.add(ticketsVO);
        }


        return ReturnData.builder().Data(ticketsVOList).build();
    }
}
